package main

//go get github.com/yuin/gopher-lua
import (
	"MqttCloudProtocolServer/publiclib/define"
	"MqttCloudProtocolServer/publiclib/logiclib"
	"flag"
	"fmt"
	"time"

	"github.com/chai2010/winsvc"
	"github.com/wonderivan/logger"

	//"log"
	"os"
	"path/filepath"
	"strings"
)

//服务名称：MQTT2TCP
//显示名称：MQTT与TCP虚拟互转
//描述：MQTT与TCP虚拟互转服务，采用本机127.0.0.1端口
var (
	appPath              string
	flagServiceName      = flag.String("service-name", "MQTT2TCP", "Set service name")
	flagServiceDesc      = flag.String("service-desc", "MQTT与TCP虚拟互转", "Set service description")
	flagServiceInstall   = flag.Bool("service-install", false, "Install service")
	flagServiceUninstall = flag.Bool("service-remove", false, "Remove service")
	flagServiceStart     = flag.Bool("service-start", false, "Start service")
	flagServiceStop      = flag.Bool("service-stop", false, "Stop service")
)

func winInit() {
	// change to current dir
	var err error
	if appPath, err = winsvc.GetAppPath(); err != nil {
		logger.Fatal(err)
	}
	logger.Info(appPath)
	if err := os.Chdir(filepath.Dir(appPath)); err != nil {
		logger.Fatal(err)
	}
	define.InitInternalPath()
	define.CheckIsLog("protocol.log")
	isrunning = true
}

//StartServer 启动服务
func StartServerWin() {
	if cloudServerContent == nil {
		GetMainClass()
	}
	cloudServerContent.Start()
	//go getCMD("ProtocolServer")
	cloudServerContent.Wait()
	isrunning = false
}

func getCMDWin(name string) {
	client := new(logiclib.RPCClientManager)
	client.Init()
	client.Start()
	for isrunning {
		cmd := client.GetCommand(&name)
		if cmd != nil {
			if cmd.Name == name {
				switch strings.ToUpper(cmd.Command) {
				case "STOP":
					StopServerWin()
					break
				}
			}
		}
		time.Sleep(time.Millisecond * 100)
	}
}

//StopServer 停止服务
func StopServerWin() {
	cloudServerContent.Stop(true)
}

func mainwin() {
	flag.Parse()

	/*// install service
	if *flagServiceInstall {
		if err := winsvc.InstallService(appPath, *flagServiceName, *flagServiceDesc); err != nil {
			log.Fatalf("installService(%s, %s): %v\n", *flagServiceName, *flagServiceDesc, err)
		}
		fmt.Printf("Done\n")
		return
	}
	// remove service
	if *flagServiceUninstall {
		if err := winsvc.RemoveService(*flagServiceName); err != nil {
			log.Fatalln("removeService:", err)
		}
		fmt.Printf("Done\n")
		return
	}
	// start service
	if *flagServiceStart {
		if err := winsvc.StartService(*flagServiceName); err != nil {
			log.Fatalln("startService:", err)
		}
		fmt.Printf("Done\n")
		return
	}
	// stop service
	if *flagServiceStop {
		if err := winsvc.StopService(*flagServiceName); err != nil {
			log.Fatalln("stopService:", err)
		}
		fmt.Printf("Done\n")
		return
	}
	// run as service
	if !winsvc.InServiceMode() {
		log.Println("main:", "runService")
		if err := winsvc.RunAsService(*flagServiceName, StartServer, StopServer, false); err != nil {
			log.Fatalf("svc.Run: %v\n", err)
		}
		return
	}*/
	// run as normal

	if *flagChildProcess {
		logger.Info("CHILD MODE")
		go getCMDWin("ProtocolServer")
	} else {
		var cmdLine string
		consoleCmd := func() {
			for isrunning && strings.ToUpper(cmdLine) != "EXIT" {
				fmt.Scanln(&cmdLine)
				time.Sleep(time.Second)
			}
			StopServerWin()
		}
		go consoleCmd()
	}
	StartServerWin()
}
